<template>
  <el-form label-width="100px" style="width: 500px;">
    <el-form-item label="头像">
      <!--头像上传开始-->
      <!--头像上传开始 action设置上传地址 limit="1"限制上传数量为1 name="file"设置上传参数名称,服务器就需要使用相同的名称-->
      <el-upload
          v-model:file-list="fileList"
          action="http://localhost:8080/v1/upload"
          limit="1"
          name="file"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
      >
        <el-dialog v-model="dialogVisible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
        <el-icon><Plus /></el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="昵称">
      <el-input placeholder="请输入昵称" v-model="user.nickname"></el-input>
    </el-form-item>
    <el-form-item label="用户名">
      <!--给用户名输入框设置了禁用(只能看不能改),所以使用属性绑定就可以不用设置双向绑定-->
      <el-input placeholder="请输入用户名" v-model="user.username" disabled></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save()">保存修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const user = ref(localStorage.user?JSON.parse(localStorage.user) : null);
const save = ()=>{
  //先暂时不管修改头像的事，后面写，这里先改昵称
  let newUser = {id:user.value.id,nickname:user.value.nickname};
  //4.处理头像上传
  if(fileList.value.length>0){
    //得到图片路径
    let url = fileList.value[0].response.data;
    console.log(url);//浏览器点击保存修改 控制台拿到/2024/02/15/4c106291-e46e-4f68-9ecf-4d5e631d58ff.png
    //将得到的图片路径装进newUser对象中：给到后端，数据库中的数据改掉
    newUser.imgUrl = url;
    //还要把图片路径放到user JS对象里,一会更新成功后存到localStorage中
    user.value.imgUrl = url;
  }
  let data = qs.stringify(newUser);
  console.log(data);
  axios.post(BASE_URL+'/v1/users/update',data).then((response)=>{
    if (response.data.code === 2000){
      ElMessage.success('修改完成!');
      localStorage.user = JSON.stringify(user.value);
      //更新完毕后刷新下页面
      location.reload();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
const fileList = ref([]);

const dialogImageUrl = ref('');
const dialogVisible = ref(false);

const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
}

const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url;
  dialogVisible.value = true;
}
</script>


<style scoped>

</style>